package bintree.nowcoder;

import java.util.Scanner;

public class BuildTreeDemo {
    static class TreeNode {
        char val;
        TreeNode left;
        TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String line = scanner.next();
            TreeNode root = build(line);
            inOrder(root);
            System.out.println();
        }
    }
    // 记录当前处理到哪个字符了
    static int index = 0;
    private static TreeNode build(String line) {
        return preOrderBuild(line);
    }

    private static TreeNode preOrderBuild(String line) {
        char c = line.charAt(index);
        if (c == '#') {
            return null;
        }
        TreeNode root = new TreeNode(c);
        index ++;
        // 继续处理左树
        root.left = preOrderBuild(line);
        index ++;
        // 继续处理右树
        root.right = preOrderBuild(line);
        return root;
    }

    private static void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
}
